//分销首页
app.controller('SoleIndex', function($rootScope, $scope, $http, $location, common, wechat){
    common.setTitle('市场中心');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    var modules     = [
        {'name'  : '教练业绩统计', 'icon'  : '../image/sole/icon-achive.png', 'url' : '/Sole/gpa'},
        {'name'  : '老师业绩统计', 'icon'  : '../image/sole/icon-achive.png', 'url' : '/Sole/gpa'},
        {'name'  : '我的二维码', 'icon'  : '../image/sole/icon-code.png', 'url' : '/User/2dcode'},
        {'name'  : '用户统计', 'icon'  : '../image/sole/icon-cal.png', 'url' : '/Sole/statis'},
        {'name'  : '收支明细', 'icon'  : '../image/sole/icon-deduct.png', 'url' : '/Sole/deduct'},
        {'name'  : '购买明细', 'icon'  : '../image/sole/icon-log.png', 'url' : '/Sole/log'},
        {'name'  : '个人信息', 'icon'  : '../image/sole/icon-user.png', 'url' : '/Sole/user_info'},
    ];
    //加载用户信息
    common.getUserInfo(function(info){
        $scope.user     = info;

        //开启角色模块
        var roleId  = $scope.user.role_id;
        //院长模块
        if(roleId == 101){
            $scope.modules  = [modules[0], modules[2], modules[3], modules[4], modules[5], modules[6]];
            $('.money-info').css('display', 'block');
        }
        //推销员a/b模块
        if(roleId == 102 || roleId == 103){
            $scope.modules  = [modules[2], modules[3], modules[4], modules[5], modules[6]];
            $('.money-info').css('display', 'block');
        }
        //推销员c/老师模块
        if(roleId == 104 || roleId == 105){
            $scope.modules  = [modules[2], modules[3], modules[6]];
        }
    });

    //打开链接
    $scope.open     = function(url){
        $location.path(url);
    }
});

//用户统计
app.controller('SoleStatis', function($rootScope, $scope, $http, $location, common, wechat){
    //初始化变量
    $scope.expand   = {'is_end' : false, 'page' : 0, 'list' : new Array(), count : 0};
    $scope.teacher  = {'is_end' : false, 'page' : 0, 'list' : new Array(), count : 0};
    $scope.parent   = {'is_end' : false, 'page' : 0, 'list' : new Array(), count : 0};
    $scope.user     = {};

    //加载JS
    $scope.init_js  = function(){
        common.setTitle('用户统计');
        wechat.wxConfig();

        //手风琴菜单监听
        $('.list-child').on('open.collapse.amui', function(){
            var dom     = $(this).find('.fa-angle-right');
            dom.addClass('fa-angle-down').removeClass('fa-angle-right');
        }).on('close.collapse.amui', function(){
            var dom     = $(this).find('.fa-angle-down');
            dom.addClass('fa-angle-right').removeClass('fa-angle-down');
        });
    }

    //加载数据
    $scope.load_data    = function(roleType){
        switch(roleType){
            //推销员
            case 2:
                if($scope.user.role_id > 101)   return true;
                var object  = $scope.expand;
                var dom     = $('.list-expand');
            break;
            //老师
            case 3:
                if($scope.user.role_id > 104)   return true;
                var object  = $scope.teacher;
                var dom     = $('.list-teacher');
            break;
            //家长
            case 4:
                var object  = $scope.parent;
                var dom     = $('.list-parent');
            break;
        }

        if(object.is_end)   return true;
        var page    = object.page + 1;
        var url     = config.prefix_url+'/Sole/childUsers?code='+config.code+
            '&type='+roleType+
            '&page='+page;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                object.page     = page;
                object.count    = info.data.count;
                object.is_end   = common.dealListInfo(info.data.list, object.list, page);
                dom.css('display', 'block');
            }
            console.log(object);
        });
    }

    common.getUserInfo(function(user){
        $scope.user     = user;
        $scope.init_js();
        $scope.load_data(2);
        $scope.load_data(3);
        $scope.load_data(4);
    })
});

//下线幸福教练绩效
app.controller('SoleGpa', function($scope, $http, $location, common, wechat){
    $scope.expandA  = {is_end : false, list : new Array(), count : 0, page : 0};
    $scope.expandB  = {is_end : false, list : new Array(), count : 0, page : 0};
    $scope.expandC  = {is_end : false, list : new Array(), count : 0, page : 0};
    $scope.keyword  = '';

    //初始化JS
    $scope.init_js  = function(){
        common.setTitle('业绩统计');
        wechat.wxConfig();

        //手风琴菜单监听
        $('.list-child').on('open.collapse.amui', function(){
            var dom     = $(this).find('.fa-angle-right');
            dom.addClass('fa-angle-down').removeClass('fa-angle-right');
        }).on('close.collapse.amui', function(){
            var dom     = $(this).find('.fa-angle-down');
            dom.addClass('fa-angle-right').removeClass('fa-angle-down');
        });

        //表单提交劫持
        $('.form-search').submit(function(){
            $scope.load_data(102, 1);
            $scope.load_data(103, 1);
            $scope.load_data(104, 1);
            return false;
        });
    }

    //加载数据
    $scope.load_data    = function(roleId, page){
        switch(roleId){
            case 102: var object    = $scope.expandA; break;
            case 103: var object    = $scope.expandB; break;
            case 104: var object    = $scope.expandC; break;
            defalut: return false; break;
        }
        if(!page)   page    = object.page + 1;
        //检测是否加载完成
        if(object.is_end && page != 1){
            common.doAlert('没有更多了!');
            return true;
        }

        var url     = config.prefix_url+'/Sole/childGpa?code='+config.code+
            '&child_role_id='+roleId+
            '&page='+page+
            '&keyword='+$scope.keyword;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                object.page     = page;
                object.is_end   = common.dealListInfo(info.data.list, object.list, object.page);
            }
        });
    }

    $scope.load_data(102);
    $scope.load_data(103);
    $scope.load_data(104);
    $scope.init_js();

});

//销售记录
app.controller('SoleLog', function($rootScope, $scope, $http, $location, common, wechat){
    //数据生命
    $scope.list     = new Array();
    $scope.nPage    = 0;
    $scope.isEnd    = false;
    $scope.keyword  = '';

    //初始化JS
    $scope.init_js  = function(){
        common.setTitle('购买明细');
        wechat.wxConfig();

        //搜索表单提交拦截
        $('.form-search').submit(function(){
            $scope.nPage    = 0;
            $scope.load_data(1);
            return false;
        });
    }

    //加载数据
    $scope.load_data    = function(){
        var page    = $scope.nPage + 1;
        var url     = config.prefix_url+'/Sole/soleLog?code='+config.code+
            '&page='+page+
            '&keyword='+$scope.keyword;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.nPage    = page;
                $scope.isEnd    = common.dealListInfo(info.data.list, $scope.list, page);
                $('.load-more').html('加载更多');
            }
        });

        $('.load-more').html('加载中...');
    }

    $scope.load_data();
    $scope.init_js();
});

//收益统计
app.controller('SoleDeduct', function($rootScope, $scope, $http, $location, common, wechat){
    common.setTitle('收入明细');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();
    //加载数据
    $scope.load_data    = function(){
        var url     = config.prefix_url+'/Sole/deductDetail?code='+config.code;
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.deduct   = info.data.info;
            }
        });
    }

    //打开链接
    $scope.open     = function(url){
        $location.path(url);
    }

    $scope.load_data();
});

//提现
app.controller('SoleWithdraw', function($rootScope, $scope, $http, $location, common, wechat){
    common.setTitle('用户提现');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();
    //用户提现
    $scope.withdraw     = function(){
        if(!confirm('是否确认提交该申请?')){
            return false;
        }
        
        var url     = config.prefix_url+'/Sole/withdraw?code='+config.code+
            '&amount='+$scope.amount;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                //更新用户信息缓存
                S('user_info', info.data.user_info);
                $scope.user     = info.data.user_info;
                common.doAlert('提现申请成功!');
                $location.path('/Sole/index');
            }
        });
    }

    //加载用户信息
    common.getUserInfo(function(userInfo){
        $scope.user     = userInfo;
        $scope.amount     = parseFloat(userInfo.wallet);
        $scope.$watch('amount', function(nVal, oVal, scope){
	    console.log('n:'+nVal+':o:'+oVal);
            var dom     = $('.am-btn');
            if(dom.attr('disabled') && nVal <= $scope.user.wallet){
                dom.removeAttr('disabled');
            }
            if(!dom.attr('disabled') && (!nVal || nVal > $scope.user.wallet) || nVal < 100 || nVal > 8000){
                dom.attr('disabled', true);
            }
        });
    });

    //加载平台配置信息以获取提现税率
    $scope.wdTaxRate    = '...';
    common.getPlatConfig(function(config){
        $scope.wdTaxRate    = config.wd_tax_rate;
    });
});

//提现记录
app.controller('SoleWDLog', function($scope, $http, $location, common, wechat){
    common.setTitle('提现记录');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    $scope.list     = new Array();
    $scope.nPage    = 1;
    $scope.isEnd    = false;
    $scope.isLoad   = false;

    //加载数据
    $scope.load_data    = function(page){
        if($scope.isLoad)    return true;

        $scope.isLoad  = true;
        var url     = config.prefix_url+'/Sole/withdrawLog?code='+config.code+
            '&page='+page;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.nPage    = page;
                $scope.isEnd    = common.dealListInfo(info.data.list, $scope.list, page);
                $('.load-more').html('加载更多');
            }
            isLoad  = false;
        });

        //加载中样式
        $('.load-more').html('加载中...');
    }

    $scope.load_data(1);
});

//用户信息
app.controller('SoleUserInfo', function($scope, $http, $location, common, wechat){
    //初始化JS
    $scope.init_js  = function(){
        common.setTitle('用户信息');
        wechat.wxConfig();
    }

    //加载数据
    $scope.load_data    = function(){
        //加载销售人员额外信息
        var url     = config.prefix_url+'/Sole/getSoleInfo?code='+config.code;
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.user     = info.data.info;
            }
            $('.btn-submit').removeAttr('disabled');
            //原用户字符链
            $scope.oldUserStr  = JSON.stringify($scope.user);
        });
    }

    //提交数据
    $scope.save_info     = function(){
        if(JSON.stringify($scope.user) == $scope.oldUserStr){
            common.doAlert('未检测到变更信息!');
            return false;
        }

        var url     = config.prefix_url+'/Sole/updateSoleInfo?code='+config.code;
        var user    = $scope.user;
        var param   = {
            'truename'  : user.truename,
            'mobile'    : user.mobile,
            'qq'    : user.qq,
            'region'    : user.region,
            'school_name'   : user.school_name,
            'school_students'   : user.school_students,
            'school_teachers'   : user.school_teachers,
            'school_stu_fee'   : user.school_stu_fee
        }
        $http.post(url, param).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                common.doAlert('信息更新成功!');
                S('user_info', null);
            }
        });
    }

    $scope.load_data();
    $scope.init_js();
})
